package com.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.dao.CourseDao;

import com.model.Course;
import com.model.User;

@Controller
public class CourseController {
    @Autowired
    CourseDao dao;
    @RequestMapping(value="/update",method=RequestMethod.GET)
   	public ModelAndView update(){
   		ModelAndView mav=new ModelAndView("/update");
   		return mav;
      }

   	@RequestMapping(value="/update",method=RequestMethod.POST)
   	public ModelAndView update( @RequestParam String coursename,
   			@RequestParam String brief) {
   		Course course=new Course();
   	
   		course.setCoursename(coursename);
   		course.setBrief(brief);
   		dao.addCourse(course);
   		 ModelAndView mav=new ModelAndView("redirect:/course");
   			return mav;
   		
   }
    @RequestMapping(value="/add",method=RequestMethod.GET)
	public ModelAndView add(){
		ModelAndView mav=new ModelAndView("/add");
		return mav;
   }

	@RequestMapping(value="/add",method=RequestMethod.POST)
	public ModelAndView add(@RequestParam String id, @RequestParam String coursename,
			@RequestParam String brief) {
		Course course=new Course();
		course.setId(id);
		course.setCoursename(coursename);
		course.setBrief(brief);
		dao.addCourse(course);
		 ModelAndView mav=new ModelAndView("redirect:/course");
			return mav;
		
}
	 @RequestMapping("/course")
		public ModelAndView course(){
			ModelAndView mav=new ModelAndView("/course");
			return mav;
	   }
	 @RequestMapping(value="/find",method=RequestMethod.GET)
		public ModelAndView order(){
			ModelAndView mav=new ModelAndView("/search");
			return mav;
	   }
		@RequestMapping(value="/find",method=RequestMethod.POST)
		public ModelAndView order(@RequestParam String id){
			   List<Course> list=dao.findByID(id);
		if(list!=null){
				ModelAndView mav=new ModelAndView("/search");
				return mav.addObject("list",list);
		}
		
			ModelAndView mav=new ModelAndView("/search");
			return mav;
		}
		@RequestMapping("/delete")
		public ModelAndView deleteByID(@RequestParam String id){
			dao.deleteCourseByID(id);
			ModelAndView modelAndView=new ModelAndView("redirect:/course");
			return modelAndView;
			
		}
		
}